package com.shawn.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.shawn.model.mongo.UserLike;
import com.shawn.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.*;

@DubboService
public class UserLikeApiImpl implements UserLikeApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 判断当前用户是否已喜欢该id
     *
     * @param userId          用户id
     * @param checkLikeUserId 检查像用户id
     * @return boolean
     */
    @Override
    public boolean whetherAlreadyLove(Long userId, Long checkLikeUserId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId))
                .addCriteria(Criteria.where("likeUserId").is(checkLikeUserId))
                .addCriteria(Criteria.where("isLike").is(true));
        return mongoTemplate.exists(query,UserLike.class);
    }

    @Override
    public void save(UserLike userLike) {
        mongoTemplate.save(userLike);
    }

    /**
     * 判断对方是否喜欢自己
     *
     * @param userId 用户id
     * @param loveId 爱人id
     * @return boolean
     */
    @Override
    public boolean checkLove(Long userId, Long loveId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(loveId))
                .addCriteria(Criteria.where("likeUserId").is(userId));
        return mongoTemplate.exists(query,UserLike.class);
    }


    /**
     * 获取关注人的数量
     * @param userId 用户id
     * @return {@link Map}<{@link String}, {@link Integer}>
     */
    @Override
    public Map<String, Integer> getFriendsDetailCount(Long userId) {
        Map<String,Integer> map = new HashMap<>();
        Query query1 = new Query();
        query1.addCriteria(Criteria.where("userId").is(userId))
                .addCriteria(Criteria.where("isLike").is(true));
        //找出所有当前用户的关注者
        List<UserLike> userLikes = mongoTemplate.find(query1, UserLike.class);

        Map<Long, UserLike> likeUserIdMap = CollUtil.fieldValueMap(userLikes, "likeUserId");

        //找出所有关注当前用户的人
        Query query2 = new Query().addCriteria(Criteria.where("likeUserId").is(userId))
                .addCriteria(Criteria.where("isLike").is(true));
        List<UserLike> userFans = mongoTemplate.find(query2, UserLike.class);

        int eachLoveCount =0;

        //遍历粉丝集合,找出用户关注
        for (UserLike ul : userFans) {
            UserLike userLike = likeUserIdMap.get(ul.getUserId());
            if (userLike!=null){
                eachLoveCount++;
            }
        }
        Integer loveCount =userLikes.size();
        Integer fanCount =userFans.size();
        map.put("eachLoveCount",eachLoveCount);
        map.put("loveCount",loveCount);
        map.put("fanCount",fanCount);
        return map;
    }

    /**
     * 获取用户信息 type
     *      * 1 互相关注
     *      * 2 我关注
     *      * 3 粉丝
     *      * 4 谁看过我
     *
     * @param userId   用户id
     * @param type     类型
     * @return {@link PageResult}<{@link UserLike}>
     */
    @Override
    public List<UserLike> getFriendsDetail(Long userId, Integer type) {
        //首先找出该用户关注的对象
        Query query1 = new Query();
        query1.addCriteria(Criteria.where("userId").is(userId))
                .addCriteria(Criteria.where("isLike").is(true));
        List<UserLike> likes = mongoTemplate.find(query1, UserLike.class);

        //找出关注该用户的对象
        Query query2 = new Query();
        query2.addCriteria(Criteria.where("likeUserId").is(userId)).addCriteria(Criteria.where("isLike").is(true));
        List<UserLike> fans = mongoTemplate.find(query2, UserLike.class);

        //找出相互关注者的信息
        Map<Long, UserLike> fansMap = CollUtil.fieldValueMap(fans, "userId");
        //遍历关注对象
        ArrayList<UserLike> feo = new ArrayList<>();
        for (UserLike like : likes) {
            if (fansMap.get(like.getLikeUserId())!=null){
                feo.add(fansMap.get(like.getLikeUserId()));
            }
        }
        switch (type){
            case 1 :
                return feo;
            case 2 :
                return likes;
            case 3 :
                return fans;
            default:
                return null;
        }
    }

    /**
     * 根据id集合获取与当前用户信息
     *
     * @param userId 用户id
     * @param ids    id
     * @return {@link List}<{@link UserLike}>
     */
    @Override
    public List<UserLike> getFriendsDetail(Long userId, List<Long> ids) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(userId))
                .addCriteria(Criteria.where("likeUserId").in(ids))
                .addCriteria(Criteria.where("isLike").is(true));
        return mongoTemplate.find(query,UserLike.class);
    }
}
